﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using GNF.Geo;
using GNF.GNFService.GNFClient;

namespace GNF.WPFControls
{
    /// <summary>
    /// a basic control that holds a GNF client and connect to the server at runtime
    /// </summary>
    public class AbstractGNFUserControl : UserControl
    {
        #region private members

        /// <summary>
        /// the GNF service client that connect to the service and pull the data
        /// </summary>
        protected GNFServiceClient _client;

        /// <summary>
        /// holds the data that comes from the GNF Service
        /// </summary>
        protected GPSData _data = new GPSData();



        #endregion

        #region contruction and init

        /// <summary>
        /// create a new client and register "AbstractGNFUserControl_Loaded" to loaded event, which connect the GNF client
        /// to the server and register the _data member to Server Publish event
        /// </summary>
        public AbstractGNFUserControl()
        {
            DataContext = _data;
            _client = new GNFServiceClient();
            Loaded += new System.Windows.RoutedEventHandler(AbstractGNFUserControl_Loaded);
        }

        /// <summary>
        /// gets a working client and connect the data to it
        /// </summary>
        /// <param name="client">connect to a working client client</param>
        public AbstractGNFUserControl(GNFServiceClient client)
        {
            if (client != null)
            {
                _client = client;
                Loaded += new System.Windows.RoutedEventHandler(AbstractGNFUserControl_Loaded);
            }
            else
            {
                throw new Exception("null reference sent");    
            }
        }

        public AbstractGNFUserControl(GPSData data) : this()
        {
            _data = data;
            DataContext = _data;
        }

        #endregion

        /// <summary>
        /// connect the client to the server and subscribe it, and register ChangeData method to the publish event
        /// </summary>
        /// <param name="sender">the sender of the event</param>
        /// <param name="e">routed args containing nothing</param>
        public virtual void AbstractGNFUserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            
            _client.Connect();
            _client.GPSDataChanged += new TimedOnGPSDataChangedEventHandler(ChangeData);
        }

        /// <summary>
        /// change _data memeber to the new "data" that was received
        /// </summary>
        /// <param name="data">the new GPS Data</param>
        /// <param name="changeTime">the time of the GPS Data measurement</param>
        private void ChangeData(GPSData data, DateTime changeTime)
        {
            _data.ChangeTo(data);
        }
  
    }
}
